package com.ztmg.water.service.file.impl;

import com.ztmg.common.constant.CommonConstants;
import com.ztmg.common.constant.ErrorConst;
import com.ztmg.common.staticRecourse.TakeFilePathAndName;
import com.ztmg.common.util.ExcelUtils;
import com.ztmg.common.util.QRcodeImage.ZXingCode;
import com.ztmg.common.util.ZipUtils;
import com.ztmg.common.util.resultUtils.JsonResult;
import com.ztmg.water.dto.base.DeliveryManDTO;
import com.ztmg.water.mapper.apartment.ApartmentRoomsMapper;
import com.ztmg.water.mapper.bills.OutPutBillsMapper;
import com.ztmg.water.mapper.order.OrderWaterMapper;
import com.ztmg.water.model.apartment.ApartmentRooms;
import com.ztmg.water.model.apartment.ApartmentRoomsExample;
import com.ztmg.water.model.bills.OutPutBills;
import com.ztmg.water.service.file.FileService;
import com.ztmg.water.vo.order.OrderWaterVO;
import org.apache.commons.codec.binary.Base64;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {
    @Resource
    private ApartmentRoomsMapper apartmentRoomsMapper;
    @Resource
    private OrderWaterMapper orderWaterMapper;
    @Resource
    private OutPutBillsMapper outPutBillsMapper;


    @Override
    public JsonResult getAllImageName(String baseUrl) {
        StringBuffer stringBuffer = new StringBuffer();
       String result = TakeFilePathAndName.getFile(baseUrl,0,stringBuffer);
       if(result.length()>0)
       {
           result = result.substring(0,result.length()-1);
       }
        baseUrl = baseUrl.replaceAll("\\\\","\\\\\\\\");
        result = result.replaceAll(baseUrl,"");
        return JsonResult.success(result);
    }

    @Override
    public JsonResult generateImage(List<Integer> ids){
        if(null==ids || ids.size()==0)
        {
            ApartmentRoomsExample example = new ApartmentRoomsExample();
            ApartmentRoomsExample.Criteria criteria =  example.createCriteria();
            criteria.andApartmentIdEqualTo(47);//查询条件赋值
            String url = CommonConstants.QRCODE_LINK_WEIXI_URK;
            /***
             * 根据具体的业务，
             */
            List<ApartmentRooms> beanList = apartmentRoomsMapper.selectByExample(example);
            //生成图片
            for (ApartmentRooms rooms:beanList)
            {
                String roomName = rooms.getRemark();
                try {
//                    String params =Base64.encodeBase64String(rooms.getRoomSn().getBytes());
                    ZXingCode.getLogoQRCodeTest(rooms.getRoomSn(),roomName);
                } catch (Exception e) {
                    return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,e.getMessage());
                }
            }
            //压缩图片文件夹
            try {
                FileOutputStream fos1 = new FileOutputStream(new File(CommonConstants.QRCODE_GENERAL_ZIP_URL));
                ZipUtils.toZip(CommonConstants.QRCODE_GENERAL_ZIP_FILE_URL, fos1,true);
            } catch (FileNotFoundException e) {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,e.getMessage());
            }
        }
        return JsonResult.success();
    }

    @Override
    public HSSFWorkbook generateExcel(String ids) {

        //excel标题
        String[] title = {"订单编号","水名称","学生名称","订水数量","订水地址","配送人员名称","回收桶数量"};



        //sheet名
         String sheetName = "订单配送表";
        HSSFWorkbook wb = null;
        if(null!=ids&&!ids.equals(""))
        {
            String[] idsArray = ids.split(",");
            DeliveryManDTO dto = new DeliveryManDTO();
            for(int i=0;i<idsArray.length;i++)
            {
                //获取数据
                OutPutBills obj = outPutBillsMapper.selectByPrimaryKey(Integer.parseInt(idsArray[i]));
                //excel文件名
                String fileName = "订单配送表"+obj.getBillSn()+".xls";
                String[] orderArray = obj.getOrderIds().split(",");
                StringBuilder orderIds = new StringBuilder();
                orderIds.append("(");
                if (orderArray!=null && !"".equals(orderArray))
                {
                    for (int x=0;x<orderArray.length;x++)
                    {
                        orderIds.append("\"");
                        orderIds.append(orderArray[x]);
                        orderIds.append("\"");
                        orderIds.append(",");
                    }
                    orderIds.delete(orderIds.length()-1,orderIds.length());
                    orderIds.append(")");
                }
                dto.setRemark(orderIds.toString());
                List<OrderWaterVO> beanList = orderWaterMapper.selectOrderListByDeliveryMan(dto);
                String content[][] = new String[beanList.size()][];
                for (int j = 0; j < beanList.size(); j++) {
                    content[j] = new String[title.length];
                    OrderWaterVO orderWaterVO = beanList.get(j);
                    content[j][0] = orderWaterVO.getOrderSn();
                    content[j][1] = orderWaterVO.getWaterName();
                    content[j][2] = orderWaterVO.getStudentName();
                    content[j][3] = String.valueOf(orderWaterVO.getWaterNum());
                    content[j][4] = orderWaterVO.getStudentAdder();
                    content[j][5] = orderWaterVO.getDeliveryName();
                    content[j][6] = String.valueOf(orderWaterVO.getWaterBucketNum());
                }
               // 生成Excel
                 wb = ExcelUtils.getHSSFWorkbook( sheetName, title, content, null);
            }

        }
                return wb;
    }


}
